import Ember from 'ember';
import InfiniteScroll from '../../infinite-scroll';

export default Ember.Controller.extend(InfiniteScroll,{
  infiniteContentPropertyName: "userCsInfo",
  infiniteModelName: "user",
  infiniteContainerName:"userCsInfoContainer",

  moment: Ember.inject.service(),
  service_PageConstrut:Ember.inject.service("page-constructure"),
  mainController: Ember.inject.controller('business.mainpage'),
  pathConfiger: Ember.inject.service("path-configer"),
  service_notification:Ember.inject.service("notification"),

  constants:Constants,
  isDisabled: true,//可修改的信息
  isDisabled3:true,//认证成功是不可修改的信息
  fileSize:"(<1M)",
  errorText:"",//上传图片错误原因
  serve:false,//认证失败中的显示、影藏
  certificationAgain:false,//再次认证
  hideButton:true,//影藏修改按钮
  showAddContents:false,//弹框
  showPopQrModal: false,//二维码弹窗控制

  uploadUrl: Ember.computed('property', function() {return this.get("pathConfiger").get("uploadUrl");}),
  isMobile: Ember.computed(function () {return this.get("global_curStatus").get("isMobile");}),  //移动端
  name:Ember.computed(function(){return this.get("model.curUser").get('name');}),
  address:Ember.computed(function(){return this.get("model.curUser").get('address');}),
  phone:Ember.computed(function(){return this.get("model.curUser").get('phone');}),
  qqnum:Ember.computed(function(){return this.get("model.curUser").get('qqnum');}),
  weixin:Ember.computed(function(){return this.get("model.curUser").get('weixin');}),
  email:Ember.computed(function(){return this.get("model.curUser").get('email');}),
  desc:Ember.computed(function(){return this.get("model.curUser").get('desc');}),
  introduce:Ember.computed(function(){return this.get("model.curUser").get('introduce');}),
  age:Ember.computed(function(){return this.get("model.curUser").get('age');}),

  idcardNum:Ember.computed(function(){return this.get("model.curUser.edinfo").get('idcardNum');}),
  zhifubao:Ember.computed(function(){return this.get("model.accountUser").get('zhifubao');}),
  cardNumber:Ember.computed(function(){return this.get("model.accountUser").get('cardNumber');}),

  nameRegular: Ember.computed('name',function(){
    var num=this.get('name');
    var Reg= /^[a-zA-Z\u4e00-\u9fa5]{2,5}$/;
    return this.Regular(num,Reg);
  }),
  idcardNumRegular:Ember.computed('idcardNum',function(){
    var num=this.get('idcardNum');
    var Reg=/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X|x)$/;
    return this.Regular(num,Reg);
  }),
  phoneRegular: Ember.computed('phone',function(){
    var num=this.get('phone');
    var Reg= /^1\d{10}/;
    return this.Regular(num,Reg);
  }),
  qqnumRegular:Ember.computed('qqnum',function(){
    var num=this.get('qqnum');
    var Reg=  /^\d{5,11}$/;
    return this.Regular(num,Reg);
  }),
  emailRegular:Ember.computed('email',function(){
    var num=this.get('email');
    var Reg=  /^\w+@[a-zA-Z0-9]+\.[a-zA-Z]+$/;
    return this.Regular(num,Reg);
  }),
  Regular:function(num,Reg){
    if (num && !(Reg.test(num))) {
      return false;
    } return true;
  },
  rate: Ember.computed(function(){
    if (this.get('model.curUser.extendInfo').get('rate')) {
      return this.get('model.curUser.extendInfo').get('rate')+"%";
    } return ;
  }),

  sexStr:Ember.computed('model.curUser.sex',function(){
    if(this.get('model.curUser.sex')==1) {
      return '男';
    } return "女";
  }),

  sexStr1:Ember.computed('model.curUser.sex',function(){
    if(this.get('model.curUser.sex')==1) {
      return '男';
    }else if(this.get('model.curUser.sex')==2){
      return "女";
    }
  }),
  sex1: Ember.computed('model.curUser.sex',function() {
    if(this.get('model.curUser.sex') === 1) {
      return true;
    }  return false;
  }),
  sex2: Ember.computed('model.curUser.sex',function() {
    if(this.get('model.curUser.sex') === 2) {
      return true;
    }return false;
  }),
  status:Ember.computed('model.curUser.status',function () {
    if (this.get('model.curUser').get('status') === 0){
      this.set('audit','');
      this.set('certificationAgain',false);
      this.set('model.curUser.verifyFailReason',"");
      return "禁用";
    }  else if (this.get('model.curUser').get('status') === 1) {
      this.set('model.curUser.verifyFailReason',"");
      this.set('audit','');
      this.set('certificationAgain',false);
      return "未认证";
    } else if (this.get('model.curUser').get('status') === 2) {
      this.set('model.curUser.verifyFailReason',"");
      this.set('audit','请耐心等待平台运营人员审核');
      this.set('certificationAgain',false);
      this.set('hideButton',false);
      return "已申请认证";
    }  else if (this.get('model.curUser').get('status') === 3) {
      this.set('model.curUser.verifyFailReason',"");
      this.set('certificationAgain',false);
      this.set('AuthenticationSuccess',true);
      this.set('audit','');
      return "认证成功";
    }  else if (this.get('model.curUser').get('status') === 4) {
      this.set('certificationAgain',true);
      this.set('audit','');
      return "认证失败";
    }
  }),
  showButton:  Ember.computed('model.curUser.status',function() {
    console.log("this.get('model.curUser').get('status')",this.get('model.curUser').get('status'));
    if(this.get('model.curUser').get('status') === 4 ){
      return 1;
    }  if(this.get('model.curUser').get('status') === 3 ){
      return 1;
    } if( this.get('model.curUser').get('status') === 2) {
      return 1;
    }  if(this.get('model.curUser').get('status') === 0){
      return 1;
    }  if(this.get('model.curUser').get('status') === 1){
      return 2;
    }
  }),
  //1到100岁
  numbers:Ember.computed(function () {
    var ary = [];
    for(var i = 1 ; i<=100; i++){
      ary.push(i);
    }
    return ary;
  }),
  modification6:Ember.computed('model.curUser.isCheckin',function(){
    if( !this.get('model.curUser').get('isCheckin')){
      this.set('modification',false);
      this.set('modification1',false);
      this.set('modification2',false);
      this.set('modification3',false);
      this.set('modification4',false);
      this.set('modification5',false);
    }
  }),
  trueOrFalse:Ember.computed('model.curUser.status',function(){
    if( this.get('model.curUser').get('status') === 2 || this.get('model.curUser').get('status') === 3 ) {
      return false;
    }else {
      return true;
    }
  }),
  statusEp2:Ember.computed('model.curUser.status',function(){
    if( this.get('model.curUser').get('status') === 2) {
      return false;
    }else {
      return true;
    }
  }),
  //上传图片不变形
  deformation:function(){
    if($(".cccc >div img").width() >= $(".cccc >div img").height()){
      $(".cccc >div ").addClass("intergration_normal");
      $(".cccc >div img").css("height", "110px  ");
      $(".cccc >div img").css("width", "auto");
      console.log("this.widthcccccccccccccc==========",$(".cccc >div img").width());
      console.log('==========cccccccccccccccintergration_normal_height',$(".cccc >div img").height(),$(".cccc >div img").width());
    }else {
      $(".cccc >div ").addClass("intergration_normal");
      $(".cccc >div img").css("width", "110px ");
      $(".cccc >div img").css("height", "auto");
      console.log("this.heightccccc==========",$(".cccc >div img").height());
      console.log('==========cccccccccccccccintergration_normal',$(".cccc >div img").height(),$(".cccc >div img").width());
    }
  },
  actions:{
    //自动生成二维码
    popQrModal: function(){
      console.log("popQrModal in");
      this.set("showPopQrModal",true);
    },
    hidden:function(){
      this.set("showPopQrModal",false);
    },
    closeQrModal: function(){
      this.set("showPopQrModal",false);
    },
    //退出登陆
    quit1: function(){
      var mainController = App.lookup("controller:business.mainpage");
      mainController.send('quit');
    },
    //修改密码
    ChangePassword: function(){
      this.set("showPopPasschangeModal",true);
    },
    // popChangePass: function(){
    //   this.set("showPopPasschangeModal",true);
    // },
    sureChangePassword: function(){
      var curUser = this.get("global_curStatus").getUser();
      var _self = this;
      var psd = this.get('password');
      var newPsd = this.get('newPass');
      var confirmPsd = this.get('confirmPass');
      if(!psd) {
        this.set("responseInfo","请输入旧密码！");
      }
      else if (!newPsd) {
        this.set("responseInfo","请输入新密码！");
      }
      else if (!confirmPsd) {
        this.set("responseInfo","请输入确认密码！");
      }
      else {
        if(!newPsd.match(/^.{6,}$/)) {
          this.set("responseInfo","新密码格式不正确,请输入6位以上字符！");
        }
        else if(newPsd!==confirmPsd) {
          this.set("responseInfo","确认密码与新密码不一致！");
        }
        else {
          var user = this.store.createRecord('userSession',{});
          user.set('id',curUser.get('id'));
          user.set('password',$.md5(psd));
          user.set('newPassword',$.md5(newPsd));
          this.get("global_ajaxCall").set("action","modifyPassword");
          user.save().then(function(userData) {
            if(userData.get('errcode')===0) {
              _self.set('responseInfo','旧密码输入错误！');
            }
            else {
              _self.store.query('localstorage/user',curUser.get('loginName')).then(function(users) {
                var user = users.get('firstObject');
                user.set('password',$.md5(newPsd));
                user.save().then(function() {
                  _self.set("showPopPasschangeModal",false);
                  _self.set('password','');
                  _self.set('newPass','');
                  _self.set('confirmPass','');
                  _self.set("responseInfo","");
                });
              });
            }
          },function() {
          });
        }
      }
      this.set("showPopPasschangeModal",false);
    },
    //年龄设置
    typeAge:function(value){
      this.set('age',value);
    },
    sexSelect:function(value) {
      this.set('sexStr',value);
    },
    //显示影藏
    VerifyFail:function () {
      this.toggleProperty('serve');
    },
    //弹框‘取消’按钮
    cancelPassSubmit: function(){


      this.set("showPopPasschangeModal",false);
      this.set("showAddContents",false);
    },
    //弹框‘确认’按钮
    changePassSubmit: function(){
      this.set("showAddContents",false);
      var model = this.get('model');
      var _self = this;
      var curUser = this.get("global_curStatus").getUser();
      if( !this.get('age') || !this.get('sexStr1') || this.get('isDisabled3')===false ||this.get('isDisabled')===false  || !this.get('name')|| !this.get('model.curUser.sexStr') || !this.get('idcardNum') ||  !this.get('phone') || !(this.get('model.curUser').get('edinfo').get('idPhoto')) || !(curUser.get('edinfo').get('idPhotoReverse')) ){
        if(!this.get('sexStr1') ){
          this.set('MustBeItem1',true);
        }else {
          this.set('MustBeItem1',false);
        }
        if(!this.get('name') ){
          this.set('MustBeItem2',true);
        }else {
          this.set('MustBeItem2',false);
        }
        if(!this.get('idcardNum') ){
          this.set('MustBeItem3',true);
        }else {
          this.set('MustBeItem3',false);
        }
        if(!this.get('phone') ){
          this.set('MustBeItem4',true);
        }else {
          this.set('MustBeItem4',false);
        }
        if(!this.get('age') ){
          this.set('MustBeItem5',true);
        }else {
          this.set('MustBeItem5',false);
        }
        console.log(" 证件照没有上传");
        this.set('MustBeItem',true);
      }
      else {
        this.set('showButton',1);
        this.set('MustBeItem',false);
        this.store.findRecord("userPrivate",model.curUser.get("id")).then(function(userEnt){
          userEnt.set("status",2);
          _self.get('global_ajaxCall').set("action",'applyVerify');
          userEnt.save();
        });
      }
    },
    statusFun:function () {
      var curUser = this.get("global_curStatus").getUser();
      this.set("showAddContents",true);
      console.log("this.get('name  sexStr idcardNum phone')=============",this.get('name') ,this.get('model.curUser.sexStr'), this.get('idcardNum'), this.get('phone'));
      if(this.get('isDisabled3')===false ||this.get('isDisabled')===false  ){
        this.set('succeed','请您先保存');
      }
      // else if( !this.get('age') || !this.get('sexStr1') || !this.get('name') || !this.get('idcardNum') ||  !this.get('phone')){
      //
      // }
      else if (!(curUser.get('edinfo').get('idPhoto')) || !(curUser.get('edinfo').get('idPhotoReverse')) ){
        this.set('succeed','*证件照没有上传哦');
      }
      else {
        this.set('succeed','认证成功后，部分信息将不能修改');
      }
    },//改变申请状态
    uploadFailImg:function(failType){
      this.set('color',true);
      if(failType===1){
        this.set("errorText","文件大小超出限制");
      }else if(failType===2){
        this.set("errorText","文件上传失败");
      }else{
        this.set("errorText","文件加载失败");
      }
    },
    uploadFailIdCardImg:function(failType){
      this.set('color1',true);
      if(failType===1){
        this.set("errorText1","文件大小超出限制");
      }else if(failType===2){
        this.set("errorText1","文件上传失败");
      }else{
        this.set("errorText1","文件加载失败");
      }
    },
    uploadFailImgReverse:function(failType){
      this.set('color2',true);
      if(failType===1){
        this.set("errorText2","文件大小超出限制");
      }else if(failType===2){
        this.set("errorText2","文件上传失败");
      }else{
        this.set("errorText2","文件加载失败");
      }
    },
    //页面跳转
    transformMine:function (menuLink) {
      console.log("transformMine in");
      this.get("mainController").switchMainPage(menuLink);
    },
    setUP:function (menuLink) {
      console.log("setUP in");
      this.get("mainController").switchMainPage(menuLink);
    },
    myWallet:function(menuLink){
      console.log("myWallet in");
      this.get("mainController").switchMainPage(menuLink);
    },
    save(){
      var model = this.get('model');
      console.log("save,model",model);
      var _self = this;
      var curUser = this.get("global_curStatus").getUser();
      console.log('check Regular nameRegular,idcardNumRegular,phoneRegular,qqnumRegular,emailRegular,cardNumberRegular is',this.get('nameRegular'),this.get('idcardNumRegular'),this.get('phoneRegular'),this.get('qqnumRegular'),this.get('emailRegular') , this.get('cardNumberRegular'));
      if(  this.get('sexStr1')  ){
        this.set('MustBeItem1',false);
      }else {
      }
      if( this.get('name') ){
        this.set('MustBeItem2',false);
      }else {
      }
      if( this.get('idcardNum') ){
        this.set('MustBeItem3',false);
      }else {
      }
      if( this.get('phone') ){
        this.set('MustBeItem4',false);
      }else {
      }
      if( this.get('age') ){
        this.set('MustBeItem5',false);
      }else {
      }
      if(  this.get('nameRegular') && this.get('idcardNumRegular') && this.get('phoneRegular') && this.get('qqnumRegular') && this.get('emailRegular')){
        this.store.query("account",{filter:{user:{id:curUser.get("id")}}}).then(function(accounts){
          var account=accounts.get("firstObject");
          account.set("zhifubao",_self.get("zhifubao"));
          // account.set("cardNumber",_self.get("cardNumber"));
          account.set("idType",47);
          account.save();
        });
        this.store.findRecord("edinfo",curUser.get("edinfo").get("id")).then(function(userEnt){
          userEnt.set("idcardNum",_self.get("idcardNum"));
          userEnt.save();
        });
        this.store.findRecord("userPrivate",curUser.get("id")).then(function(userEnt){
          model.cstag.forEach(function(lan) {
            if(lan.get('checked')){
              userEnt.get('cstag').pushObject(lan);
            }
            if(lan.get('checked')===false) {
              userEnt.get('cstag').removeObject(lan);
            }
          });
          model.language.forEach(function(lan) {
            if(lan.get('checked')){
              userEnt.get('language').pushObject(lan);
            }
            if(lan.get('checked')===false) {
              userEnt.get('language').removeObject(lan);
            }
          });
          if(_self.get('sexStr')==1 || _self.get('sexStr')==2){
            userEnt.set('sex',_self.get('sexStr'));
          }
          userEnt.set('name',_self.get('name'));
          userEnt.set("address",_self.get("address"));
          userEnt.set("age",_self.get("age"));
          userEnt.set("phone",_self.get("phone"));
          userEnt.set("weixin",_self.get("weixin"));
          userEnt.set("email",_self.get("email"));
          userEnt.set("desc",_self.get("desc"));
          userEnt.set("qqnum",_self.get("qqnum"));
          userEnt.set("introduce",_self.get("introduce"));
          userEnt.save();
        });
        //修改签入状态，并跳转
        this.set("isDisabled",true);//文本可编辑
        this.set('isDisabled3',true);
        model.curUser.set("isCheckin",false);//可见页面
      }else {
        if(this.get('nameRegular')===false){
          this.set('modification',true);
        }else {
          this.set('modification',false);
        }  if(this.get('idcardNumRegular')===false){
          this.set('modification1',true);
        }else {
          this.set('modification1',false);
        }  if(this.get('phoneRegular')===false){
          this.set('modification2',true);
        }else {
          this.set('modification2',false);
        }  if(this.get('qqnumRegular')===false){
          this.set('modification3',true);
        }else {
          this.set('modification3',false);
        }  if(this.get('emailRegular')===false){
          this.set('modification4',true);
        }else {
          this.set('modification4',false);
        }
      }

    },//确定

    uploadSucc: function(response) {
      this.deformation();
      var model = this.get('model');
      var res=JSON.parse(response);
      this.store.findRecord("userPrivate",model.curUser.get("id")).then(function(userEnt){
        userEnt.set("avatar",res.relativePath);
        console.log("res.relativePath:",res.relativePath);
        userEnt.save();
      });
      this.set('color',false);
      this.set("errorText","");
    },
    //上传证件照
    uploadidcardImg: function(response) {
      // this.deformation();
      var curUser=this.get('global_curStatus').getUser();
      var res=JSON.parse(response);
      this.store.findRecord("edinfo",curUser.get('edinfo').get('id')).then(function(userEnt){
        userEnt.set("idPhoto",res.relativePath);
        userEnt.save();
      });
      this.set('color1',false);
      this.set("errorText1","");
    },
    //上传证件照反面
    uploadidcardImgReverse:function(response){
      // this.deformation();
      var res=JSON.parse(response);
      var curUser=this.get('global_curStatus').getUser();
      this.store.findRecord("edinfo",curUser.get("edinfo").get("id")).then(function(userEnt){
        userEnt.set("idPhotoReverse",res.relativePath);
        userEnt.save();
        console.log("res.relativePath",res.relativePath);
      });
      this.set("errorText2","");
      this.set('color2',false);
    }
  },
});
